<?php

/**
 * 商品管理
 * File Name：GoodsController.php
 * File Encoding：UTF-8
 * File New Time：2014-5-19 17:49:56
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class GoodsController extends AdminBase {

    //商品列表
    public function actionIndex() {
        //搜索
        if (IS_POST) {
            foreach ($_POST as $k => $v) {
                if ($v == '') {
                    unset($_POST[$k]);
                }
            }
            $this->redirect(AdminBase::U('index', $_POST));
            exit;
        }
        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //分页数
        $limit = 20;
        //报名模型
        $model = GoodsModel::model();
        //条件数组
        $where = $this->setWhere();
        $criteria = $model->where($where);
        //信息总数
        $count = $model->count($criteria);
        //分页
        $page = self::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $data = $model->findAll($criteria);

        $this->assign("Page", $page->show());
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->render('index');
    }

    //添加商品
    public function actionAdd() {
        if (IS_POST) {
            if (GoodsModel::model()->add($_POST)) {
                $this->success('添加商品成功！', self::U('index'));
            } else {
                $error = GoodsModel::model()->getOneError();
                $this->error($error ? $error : '添加商品失败！');
            }
        } else {
            $this->render();
        }
    }

    //编辑
    public function actionEdit() {
        if (IS_POST) {
            if (GoodsModel::model()->editor($_POST)) {
                //推广人，超管才可以
                if (Yii::app()->passport->isAdministrator()) {
                    $data = GoodsModel::model()->findByPk($_POST['goods_id']);
                    if (isset($_POST['recommend']) && $_POST['recommend'] && !empty($data)) {
                        GoodsSpreadModel::model()->saveSpreadUser($data->goodsbm_id, $_POST['recommend']);
                    } else {
                        GoodsSpreadModel::model()->deleteSpread($data->goodsbm_id);
                    }
                }
                $this->success('修改成功！', self::U('index'));
            } else {
                $error = GoodsModel::model()->getOneError();
                $this->error($error ? $error : '修改失败！');
            }
        } else {
            $goods_id = Yii::app()->request->getParam('goods_id');
            //查询报名信息
            $data = GoodsModel::model()->findByPk($goods_id);
            if (empty($data)) {
                if (IS_AJAX && $_GET['ajax']) {
                    exit('no');
                } else {
                    $this->error('该报名信息不存在！');
                }
            }
            $this->assign('data', $data);
            if ($_GET['ajax']) {
                $this->render('ajax_editor');
            } else {
                //推广人信息
                if (Yii::app()->passport->isAdministrator()) {
                    $recommend = GoodsSpreadModel::model()->find(GoodsBmModel::model()->where(array('goods_bm_id' => $data->goodsbm_id)));
                    if (!empty($recommend)) {
                        $recommend = $recommend->userid;
                        $this->assign('recommend', $recommend);
                    }
                }
                $this->render();
            }
        }
    }

    //删除信息
    public function actionDelete() {
        $goods_id = Yii::app()->request->getParam('goods_id');
        //查询报名信息
        $data = GoodsModel::model()->findByPk($goods_id);
        if (empty($data)) {
            $this->error('该信息不存在！');
        }
        //不删除商品，删除直接以下架处理
        $data->status = 0;

        if ($data->save()/* GoodsModel::model()->goodsDel($data) */) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    //快捷编辑后，ajax重新加载内容
    public function actionPublic_load() {
        $goods_id = Yii::app()->request->getParam('goods_id');
        //查询报名信息
        $data = GoodsModel::model()->findByPk($goods_id);
        if (empty($data)) {
            exit('');
        }
        $this->assign('rs', $data);
        $this->render();
    }

    //一键删除已过期商品，也就是根据下架时间，推前3天
    public function actionBatch_expired() {
        $this->success('该功能暂时关闭！');
        exit;
        $where = array(
            'end_time' => array('LT', time() - (86400 * 3)),
        );
        $model = GoodsModel::model();
        $data = $model->findAll($model->where($where));
        if (!empty($data)) {
            foreach ($data as $rs) {
                $model->goodsDel($rs);
            }
        }
        $this->success('批量删除已过期商品操作成功！');
    }

    //取消活动
    public function actionActivity() {
        $goods_id = Yii::app()->request->getParam('goods_id');
        $model = GoodsModel::model();
        if ($model->activity($goods_id) !== false) {
            $this->success('取消活动成功！');
        } else {
            $error = $model->getOneError();
            $this->error($error ? $error : '取消活动失败！');
        }
    }

    /**
     * 设置查询条件
     * @param array $newWhere 额外附件条件
     * @return array
     */
    private function setWhere($newWhere = array()) {
        //条件数组
        $where = array(
            'order' => 'listorder  desc,goods_id desc',
        );
        //当前时间戳
        $time = time();
        //状态
        $status = $_GET['status'];
        if ($status != '') {
            switch ((int) $status) {
                case 0://已下架
                    $where['status'] = array('EQ', $status);
                    break;
                case 1://正在显示
                    $where['status'] = array('EQ', $status);
                    $where['start_time'] = array('LT', strtotime(date('Y-m-d', $time)) + 86400);
                    $where['end_time'] = array('GT', $time);
                    break;
                case 2://已过期
                    //网站配置
                    $config = ConfigModel::model()->getConfig();
                    if (isset($config['expire_time']) && $config['expire_time'] > 0) {
                        $time = $time - $config['expire_time'] * 24 * 60 * 60;
                    } else {
                        $time = $time - 2 * 24 * 60 * 60;
                    }
                    $where['end_time'] = array('LT', $time);
                    break;
                case 3://所有显示
                    $where['status'] = 1;
                    $where['end_time'] = array('GT', $time);
                    break;
                case 4://即将显示
                    $where['status'] = 1;
                    $where['start_time'] = array('GT', $time);
                    break;
            }
        } else {
            unset($_GET['status']);
        }
        //时间
        if ((int) $status < 2) {
            $start_time = strtotime($_GET['start_time']);
            $end_time = 0;
            if ($start_time) {
                $where['start_time'] = array('EGT', $start_time);
            }
            if ($_GET['end_time']) {
                $end_time = strtotime($_GET['end_time']) + 86399;
            }
            if ($end_time && $end_time > $start_time) {
                $where['start_time'] = array(array('EGT', $start_time), array('ELT', $end_time), 'and');
            } else if ($end_time && $end_time <= $start_time) {
                $where['start_time'] = array(array('EGT', $start_time), array('ELT', $start_time + 86399), 'and');
            }
        }
        //栏目
        $cat_id = $_GET['cat_id'];
        if ($cat_id != '') {
            //子栏目列表
            $childid = CategoryModel::model()->getArrChildid($cat_id);
            $where['cat_id'] = array('IN', $childid);
        } else {
            unset($_GET['cat_id']);
        }
        //Level
        $level = $_GET['level'];
        if ($level != '') {
            $where['level'] = array('EQ', (int) $level);
        } else {
            unset($_GET['level']);
        }
        //店铺类型
        $item_type = $_GET['item_type'];
        if ($item_type != '') {
            $where['item_type'] = array('EQ', (int) $item_type);
        } else {
            unset($_GET['item_type']);
        }
        //关键字
        $keyword = trim($_GET['keyword']);
        if ($keyword) {
            //查找类型
            $type = (int) $_GET['type'];
            switch ($type) {
                case 1://商品ID
                    $where['product_id'] = array('EQ', $keyword);
                    break;
                case 2://商品名称
                    $where['goods_name'] = array('LIKE', "%{$keyword}%");
                    break;
                case 3://商品旺旺
                    $where['nick'] = array('LIKE', "%{$keyword}%");
                    break;
                default:
                    $where['product_id'] = array('EQ', $keyword);
                    break;
            }
        }

        //默认情况下隐藏已下架
        if (!isset($_GET['keyword']) && !isset($where['status'])) {
            $where['status'] = array('GT', 0);
        }

        if (!empty($newWhere) && is_array($newWhere)) {
            $_GET = array_merge($_GET, $newWhere);
            $where = array_merge($where, $newWhere);
        }
        $this->assign($_GET);
        return $where;
    }

}
